<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item> <i class="el-icon-lx-cascades"></i> 薪资管理 </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button type="primary" icon="el-icon-delete" class="handle-del mr10" @click="delAllSelection">批量删除 </el-button>
                <el-button type="primary" icon="el-icon-plus" class="handle-del mr10" @click="addSalary"> 结算本月薪资 </el-button>
                <el-button type="primary" icon="el-icon-download" class="handle-del mr10" @click="handleExport">导出全部</el-button>
                <el-input v-model="query.name" placeholder="员工名" class="handle-input mr10"></el-input>
                <el-date-picker v-model="query.month" type="month" placeholder="选择月"></el-date-picker>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
            </div>
            <el-table
                :data="tableData"
                v-loading="loading"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" align="center"></el-table-column>
                <el-table-column prop="empId" label="员工编号" align="center"></el-table-column>
                <el-table-column prop="name" label="员工名" align="center"></el-table-column>
                <el-table-column prop="month" label="月份" align="center"></el-table-column>
                <el-table-column prop="workDate" label="工作时间" align="center"></el-table-column>
                <el-table-column prop="baseSalary" label="基本薪资" align="center"></el-table-column>
                <el-table-column prop="errandSubsidys" label="出差补贴" align="center"></el-table-column>
                <el-table-column prop="fullAttendance" label="全勤" align="center"></el-table-column>
                <el-table-column prop="sb" label="社保" align="center"></el-table-column>
                <el-table-column prop="tax" label="税务" align="center"></el-table-column>
                <el-table-column prop="fine" label="罚款" align="center"></el-table-column>
                <el-table-column prop="finalSalary" label="实发薪资" align="center"></el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="jumper,total, prev, pager, next, sizes"
                    :page-sizes="[5, 10, 20, 30]"
                    @size-change="handleSizeChange"
                    :current-page="query.pageNo"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>
        <!-- 编辑弹出框 -->
        <el-dialog :title="title" :visible.sync="open" width="80%">
            <div v-html="content"></div>
        </el-dialog>
    </div>
</template>

<script>
import { salaryData, add, batchDelete,exportSalary} from '../../api/salary';

export default {
    name: 'salary',
    data() {
        return {
            query: {
                name: '',
                month: '',
                pageNo: 1,
                pageSize: 5
            },
            tableData: [],
            loading: true,
            multipleSelection: [],
            delList: [],
            editVisible: false,
            pageTotal: 0,
            idx: -1,
            id: -1,
            title: '',
            open: false,
            content: ''
        };
    },
    created() {
        this.getData();
    },
    methods: {
        // 获取 easy-mock 的模拟数据
        getData() {
            this.loading = true;
            salaryData(this.query).then((res) => {
                this.tableData = res.data.data;
                this.pageTotal = res.data.count;
                this.loading = false;
            });
        },
        // 触发搜索按钮
        handleSearch() {
            this.$set(this.query, 'pageNo', 1);
            this.getData();
        },
        // 删除操作
        handleDelete(index, row) {
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            }).then(() => {});
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        delAllSelection() {
            const length = this.multipleSelection.length;
            let str = '';
            this.delList = this.delList.concat(this.multipleSelection);
            for (let i = 0; i < length; i++) {
                str += this.multipleSelection[i].salaryId + ',';
            }
            this.$confirm('您确定要删除选中数据吗?', '提示', {
                type: 'warning'
            }).then(() => {
                batchDelete(str).then((res) => {
                    this.getData();
                    this.$message.success(res.data);
                });
            });
            this.multipleSelection = [];
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'pageNo', val);
            this.getData();
        },
        handleSizeChange(val) {
            this.query.pageSize = val;
            this.getData();
        },
        addSalary() {
            add().then((res) => {
                this.getData();
                this.$message.success(res.data);
            });
        },
        handleExport() {
            this.$confirm('是否确认导出当前月份薪资?', '警告', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })
                .then(function () {
                    return exportSalary();
                })
                .then((response) => {
                    this.downloadFile(response, '薪资表');
                });
        }
    }
};
</script>

<style scoped>
body {
    margin: 0;
}
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}

.table {
    width: 100%;
    font-size: 14px;
}

.red {
    color: #ff0000;
}

.mr10 {
    margin-right: 10px;
}

.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
